/* 
	PROJECT: scsu-csc653-chatter
	ABOUT: global variables, function, startup script, and event bindings
	DATE: 09-2010
	AUTHORS: skenny214@gmail.com, jinhongli08@gmail.com, hourigan.brian@gmail.com, yaqiluo2010@gmail.com
*/

//global variables which contains the user that logged in using the dialog form

var currentUsername;
var chatterClientApplet;

//startup functions
$(function() {
	//assign the applet id to a named javascript variable
	chatterClientApplet = document.chatter_client_applet;

	//start the login dialog
	$('#start-chatting-button').button().click(onStartChattingButtonClick);
	$('#login-dialog').dialog({
		modal:true, 
		resizable: false, 
		close: onLoginDialogClose
	});
	
	//register logout button
	$('#logout-button').button().click(onLogoutButtonClick);
	
});


//listen for login dialog button click
function onStartChattingButtonClick(){
	//get the username and password from the dialog
	var username = $('#username-text-input').val();
	var password = $('#password-text-input').val();
	var host = $('#host-text-input').val();
	
	//ask the chatter applet to log us in
	currentUsername = username;
	chatterClientApplet.chatterClient.login(username, password, host);
	
}

function onLogoutButtonClick(){
	chatterClientApplet.chatterClient.chatterOutput.logout(currentUsername);
}

//listens for login dialog close event and reopens the dialog...so no closing..only destroying
function onLoginDialogClose(){
	$('#login-dialog').dialog({modal:true, resizable: false})
}


//calls this no successful login
function chatterClientLoginSuccess(){
	$('#login-dialog').dialog('destroy');
}

//calls this on failure of login
function chatterClientLoginFailure(){
	$('#disconnect-error-message').hide();
	$('#login-error-message').fadeIn();
}

//called to reopen login dialog box (with error message) when the server disconnects for varous reasons
function chatterClientDisconnect(){
	$('#login-error-message').hide();
	
	$('#login-dialog').dialog({
		modal:true, 
		resizable: false, 
		close: onLoginDialogClose
	});

	$('#disconnect-error-message').fadeIn();
}



//applet communication handler. is called when an input event occurs from the server 
// note - it receives a raw server messsage and simply parses it on the fly
function onChatterClientInput(input){
	//split the input line
	parameters = input.split('%%');
	
	//get the message category and then its value
	typeArray = parameters[1].split('=');	
	typeCategory = typeArray[0];
	typeValue = typeArray[1];
	
	//proceed for each category of input types...
	if(typeCategory == 'Broadcast'){
		
		if(typeValue == 'message'){
			var username = parameters[2];
			var message = parameters[3];
			addMessage(username, message);
			
		}else if(typeValue == 'userlist'){
			var usernames = parameters[2].split(',');
			refreshUserList(usernames);
		
		}
		
	}else if(typeCategory == 'Operation'){
		if(typeValue == 'login'){
			var status = parameters[2].split('=')[1];
			
			if(status == 'success'){
				 chatterClientLoginSuccess();
			}else{
				chatterClientLoginFailure()
			}
		
		}else if(typeValue == 'logout'){
			chatterClientDisconnect();
		}

	}

	
}


